<!--
 * @Description:: 请补充填写
 * @Version: 1.0
 * @Author: 小白
 * @Date: 2022-04-14 14:26:46
 * @LastEditors: 小白
 * @LastEditTime: 2025-11-20 11:39:37
 * @FilePath: \tmsadmin\src\views\contract\staff\components\list.vue
-->

<template>
  <SearchForm @search="searchFun" :houseList="houseList" style="margin-bottom: 20px" />
  <el-card class="page_card" shadow="never">
    <template #header>
      <div class="card-header">
        <div class="left">
          合同管理
         
            <el-button style="margin-left: 20px" type="primary" @click="add('导入')">
              导入数据
            </el-button>
            <el-button style="margin-left: 20px" type="warning" @click="edits('新增员工信息')">
              新增员工信息
            </el-button>
            <!-- 按钮组 编辑 删除 重置密码 -->
            <el-button type="success" @click="edit('编辑员工信息')" style="margin-left: 20px">
              编辑员工信息
            </el-button>
            <el-button type="danger" @click="showDelete('删除员工信息')" style="margin-left: 20px">
              删除员工信息
            </el-button>
            <el-button type="warning" @click="repeatPwd('重置密码')" style="margin-left: 20px">
              重置密码
            </el-button>
        </div>
        <div class="right">
          <el-button circle @click="refreshFun">
            <i class="icon ion-refresh" :class="iconRefresh" />
          </el-button>
        </div>
      </div>
    </template>
    <Table :table-data="tableData" :table-columns="columns" :page-size="pageSize" :page-num="pageNum" :loading="loading"
      :total="total" @edit="edit" @selectionChange="selectionChange" @change="changePage" @ok="getData()" />
    <Modal @ok="getData()" ref="modalRef"></Modal>
    <Modals @ok="getData()" ref="modalRefs"></Modals>

    <ModalUpload @ok="getData()" ref="modalUploadRef"></ModalUpload>
    <div class="pdf">

    </div>
  </el-card>
</template>
<script lang="ts" setup>
import { REQUEST } from '@/api/request'
import { ElMessage, ElMessageBox } from 'element-plus'
import SearchForm from './search.vue'
import Table from './table.vue'
import Modal from './modal.vue'
import Modals from './modals.vue'
import { saveAs } from "file-saver";
import ModalUpload from './modalupload.vue'
import { onMounted, ref } from 'vue'
const queryForm = ref({})
const iconRefresh = ref('')

const pageNum = ref(1)
const pageSize = ref(10)
import { store } from '@/store'
import axios from 'axios'
import { getToken } from '@/plugins/cookie'
import HTTP_URI from '@/api/base_url'
const total = ref(0)
const modalUploadRef = ref(null)
const modalRef = ref(null)
const modalRefs = ref(null)

const loading = ref(false)
const tableData = ref([])
const columns = ref([
  {
    title: '乙方名称',
    prop: 'staff',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  },
  {
    title: '银行卡号',
    prop: 'postalAccount',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '开户行',
    prop: 'accountName',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '车主',
    prop: 'carOwner',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '住址',
    prop: 'address',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },

  {
    title: '联系方式',
    prop: 'phone',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '证件号码',
    prop: 'idNumber',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '车牌号',
    prop: 'vehicleNumber',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '车长',
    prop: 'vehicleCommander',
    align: 'center',
    modifiable: true,
    isShow: true,
    tooltip: false,
    minwidth: '180'
  },
  {
    title: '创建时间',
    prop: 'createTime',
    align: 'center',
    modifiable: true,
    isShow: true,
    minwidth: '180'
  }
])
const emits = defineEmits(['go'])
onMounted(() => {
  getData()
  getDatas()
})
const look = () => {
  if (selectList.value.length > 0) {

    const ids = selectList.value.map((el: any) => {
      return el.userId
    })
    emits('go', { view: 'ActiveDetails', params: { row: { id: ids } } })
  } else {
    ElMessage({
      type: 'warning',
      message: '请勾选数据！'
    })
  }
}
const setContract = (title: string) => {
  if (selectList.value.length > 0) {
    modalRefs.value.openModal('生成合同', selectList.value)

  } else {
    ElMessage({
      type: 'warning',
      message: '请勾选数据！'
    })
  }
}
const contractTemplate = ref('')
const getDatas = () => {
  REQUEST.POST('OFFICIALINFO', {
    id: store.state.userInfo.houseId
  }).then((res: any) => {
    if (res.status == 200) {
      contractTemplate.value = res.data.contractTemplate
    }
  })
}

const repeatPwd = () => {
  if (selectList.value.length > 0) {
    ElMessageBox.confirm('重置密码与账号一致，请确认', '提示', {
      distinguishCancelAndClose: true,
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(() => {
      let arr:any = []
      selectList.value.map((item: any) => {
          const obj = {
            id:item.userId,
            password:item.phone
          }
          arr.push(obj)
      }) 
      REQUEST.POST('USERBATHEDIT', { updateReqList: arr }).then((res: any) => {
        if (res.status === 200) {
          ElMessage({
            type: 'success',
            message: '成功'
          })
          getData()
        } else {
          ElMessage({
            type: 'warning',
            message: res.message
          })
        }
      })
    })
  } else {
    ElMessage({
      type: 'warning',
      message: '请勾选数据！'
    })
  }
}
const houseList = ref([])
const add = (title: string) => {
  modalUploadRef.value.openModal('导入员工信息')
}
const showDelete = (row: any) => {
  if (selectList.value.length > 0) {
    const ids = selectList.value.map((item: any) => item.id)
    const userIds = selectList.value.map((item: any) => item.userId)
    ElMessageBox.confirm('您将进行删除操作，请确认', '提示', {
      distinguishCancelAndClose: true,
      confirmButtonText: '确定',
      cancelButtonText: '取消'
    }).then(() => {
      REQUEST.POST('USERDEL', {
        ids: userIds
      }).then((res: any) => {

      })
      REQUEST.POST('STAFFDEL', {
        ids: ids
      }).then((res: any) => {
        if (res.status === 200) {
          ElMessage({
            type: 'success',
            message: '成功删除'
          })
          getData()
        } else {
          ElMessage({
            type: 'warning',
            message: res.message
          })
        }
      })
    })
  } else {
    ElMessage({
      type: 'warning',
      message: '请勾选数据！'
    })
  }
}
const getData = () => {
  // loading.value = true
  REQUEST.POST('STAFFLIST', {
    ...queryForm.value,
    houseId: store.state.userInfo.houseId,
    pageNumber: pageNum.value, pageSize: pageSize.value
  }).then((res: any) => {
    tableData.value = res.data.records
    total.value = JSON.parse(res.data.total)
  })
}
const modalRefYhq = ref()
const refreshFun = () => {
  iconRefresh.value = 'iconRefresh'
  getData()
  setTimeout(() => {
    iconRefresh.value = ''
  }, 1000)
}
const searchFun = (e: any) => {
  queryForm.value = e
  getData()
}
const selectList = ref([])
const selectionChange = (e: any) => {
  selectList.value = e
}

const edit = () => {
  if (selectList.value.length !== 1) {
    ElMessage.error('请选择一条数据进行编辑')
    return
  } else {
    modalRef.value.openModal('编辑员工信息', houseList.value, selectList.value[0])
  }
}
const edits = ()=>{
   modalRef.value.openModal('新增员工信息', houseList.value)
}
const changePage = (e: any) => {
  pageNum.value = e.pageNum
  pageSize.value = e.pageSize
  getData()
}
</script>

<style lang="scss" scoped>
.card-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
}
</style>
